#!/usr/bin/python
#-*- coding: utf-8 -*-

# subversion info:
# $HeadURL: $
# $Author: $
# $Id: $
# $Revision: $

"""
  htpc-event: Htpc event management script.
  Authors = Rafael Treviño <skasi.7@gmail.com>
  Date = 24/02/10
"""

# Imports externals

# Imports internals
from HtpcUtils.App import App
from HtpcUtils.EventManager import EventManager


# Main entry point
if __name__ == '__main__':
  app = App('htpc-event')
  app.add_option('--add', dest='add',
      help='add a new event to the event list',
      action='store_true', default=False)
  app.add_option('--delete', dest='delete',
      help='delete an event from the event list',
      action='store_true', default=False)
  app.add_option('--list', dest='list',
      help='list all the events in the event list',
      action='store_true', default=False)
  (params, args) = app.parse_args()

  app.updateConfig(app.parseConfigFilename('/etc/htpc-utils.yml') or {})
  eventManagerConfig = app.getConfig().get('EventManager',
          {'filename': 'test.rss'})
  eventManager = EventManager(
      eventManagerConfig.get('filename', 'test.rss'),
      eventManagerConfig.get('link'))

  if params.list:
    for event in eventManager.eventList():
      print event
    app.exit(0)

  if params.add:
    if len(args) != 2:
      app.getLogger().error('Invalid number of parameters')
      app.exit(1)
    title = args.pop(0)
    description = args.pop(0)

    eventManager.add(title, description)
    app.exit(0)

  if params.delete:
    if len(args) != 1:
      app.getLogger().error('Invalid number of parameters')
      app.exit(1)
    title = args.pop(0)

    eventManager.delete(title)
    app.exit(0)

  app.getLogger().error('No operation provided')

